package com.example.myplayerlib.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.Toast;

import com.example.myplayerlib.R;
import com.example.myplayerlib.player.IMediaPlayer;
import com.example.myplayerlib.player.MyPlayer;

import java.io.IOException;

/**
 * Created by Administrator on 2017/12/10.
 */

public class MyPlayerView extends FrameLayout implements MediaController.MediaPlayerControl{
    private SurfaceView surfaceView;
    private IMediaPlayer mediaPlayer;
    private String mUri;
    private MediaController mMediaController;

    public MyPlayerView(@NonNull Context context) {
        this(context,null,0);
    }

    public MyPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        findViews();
        initListener();
    }

    private void findViews() {
        View.inflate(getContext(),R.layout.layout_my_player_view,this);
        surfaceView=findViewById(R.id.surfaceView);
    }

    private void initListener() {
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if(null==mediaPlayer){
                    openVideo();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });
    }
    public void setVideoPath(String path){
        this.mUri = path;
//        openVideo();
    }
    private void openVideo() {
        //真正打开播放器
        if(TextUtils.isEmpty(this.mUri)){
            return;
        }
        mediaPlayer = new MyPlayer();
        try {
            mediaPlayer.setDataSource(this.mUri);
            mediaPlayer.setSurface(surfaceView.getHolder().getSurface());
            mediaPlayer.prepareAsync();
            if (mMediaController == null) {
                mMediaController = new MediaController(((Activity)getContext()));
                mMediaController.setMediaPlayer(this);
                mMediaController.setAnchorView(this);
                mMediaController.setEnabled(true);
                mMediaController.show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void start() {
        mediaPlayer.start();
    }

    @Override
    public void pause() {

    }

    @Override
    public int getDuration() {
        return 0;
    }

    @Override
    public int getCurrentPosition() {
        return 0;
    }

    @Override
    public void seekTo(int i) {

    }

    @Override
    public boolean isPlaying() {
        return false;
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return false;
    }

    @Override
    public boolean canSeekBackward() {
        return false;
    }

    @Override
    public boolean canSeekForward() {
        return false;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        toggleMediaControlsVisiblity();
        return super.onTouchEvent(event);
    }
    private void toggleMediaControlsVisiblity() {
        if (mMediaController.isShowing()) {
            mMediaController.hide();
        } else {
            mMediaController.show();
        }
    }
}
